<?php

namespace Fendx;

use Doctrine\ORM\EntityManager;
use Fendx\Doctrine\EntityManagerOverride;

class Db
{

    /**
     * 只读库实体管理
     *
     * @return EntityManager
     */
    public static function getREntityManager(string $db = 'default'): EntityManagerOverride {
        static $map = null;
        if (!isset($map[$db])) {
            $Db_Module = "\\Fendx\\Db\\EntityManager";
            $map[$db] = $Db_Module::Factory('r', $db);
        }
        $map[$db]->checkConnection();
        return new EntityManagerOverride($map[$db]->getEntityManager(), $map[$db]);
    }

    /**
     * 读写库实体管理
     *
     * @return EntityManager
     *
     */
    public static function getWEntityManager(string $db = 'default'): EntityManagerOverride {
        static $map = null;
        if (!isset($map[$db])) {
            $Db_Module = "\\Fendx\\Db\\EntityManager";
            $map[$db] = $Db_Module::Factory('w', $db);
        }
        $map[$db]->checkConnection();
        return new EntityManagerOverride($map[$db]->getEntityManager(), $map[$db]);
    }

    /**
     * 读写库实体管理
     *
     * @return EntityManager
     *
     */
    public static function getWOriginEntityManager(string $db = 'default'): EntityManager {
        static $map = null;
        if (!isset($map[$db])) {
            $Db_Module = "\\Fendx\\Db\\EntityManager";
            $map[$db] = $Db_Module::Factory('w', $db);
        }
        $map[$db]->checkConnection();
        return $map[$db]->getEntityManager();
    }
}
